package com.yedean.redis;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import javax.annotation.Resource;

/**
 * RedisConfig
 *
 * @author yedean
 * @date 2022/10/10
 */
@Configuration
public class RedisConfig {

    @Resource
    private RedisConnectionFactory factory;

    /**
     * 功能:为容器创建一个RedisTemplate
     *
     * @author: syy
     * @return redisTemplate
     */
    @Bean("redisTemplate")
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        //key采用String的序列化方式
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        //hash的key采用String的序列化方式
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        //hash的value采用String的序列化方式
        redisTemplate.setHashValueSerializer(new StringRedisSerializer());
        //value采用String的序列化方式
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        redisTemplate.setConnectionFactory(factory);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}
